<?php


namespace app\api\controller;


use think\Exception;
use think\exception\ValidateException;
use think\facade\Cache;
use think\facade\Log;
use think\facade\Validate;
use app\common\business\User as BusinessUser;

class User extends Auth
{
    public function index()
    {
        $user = (new BusinessUser())->getNormalUserById($this->user_id);
        return show(1, $user);
    }


    public function update()
    {
        $sex = input('sex', 0, 'intval');
        $username = request()->param('username');
        $data = [
            'username' => $username,
            'sex' => $sex,
        ];

        try {
            validate(\app\api\validate\User::class)->scene('update')->check($data);
        } catch (ValidateException $e) {
            return show(2, $e->getMessage());
        }

        $res = $user = (new BusinessUser())->update($this->user_id, $data, $this->AccessToken);
        if (!$res) {
            return show(2, '更新失败');
        }

        return show(1, '更新成功');
    }
}